﻿/*
(c) 2010 - Open Source Business Foundation e. V. (www.osbf.de)
This software is provided under Microsoft Reciprocal License (Ms-RL)
(http://www.microsoft.com/opensource/licenses.mspx#Ms-RL)
All other rights reserved
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;

namespace MicroFormatProcessing
{
    /// <summary>
    /// This class represents a Tag for a Microformat. Each reference of the tag is stored in references (rowKey of the referencing microformat)
    /// </summary>
    
    public class Tag : Microsoft.WindowsAzure.StorageClient.TableServiceEntity
    {

        public string References { get; set; }

        private HashSet<string> ReferenceSet;

        public Tag() : base()
        {
            Init();
        }

        public Tag(string userId, string tagName) : base(userId, tagName)
        {
            Init();
        }

        private void Init()
        {
            this.ReferenceSet = new HashSet<string>();
        }

        public string GetUserId()
        {
            return this.PartitionKey;
        }

        public string GetTagName()
        {
            return this.RowKey;
        }

        public int GetReferenceCount()
        {
            return this.ReferenceSet.Count;
        }

        public bool HasReference(string reference)
        {
            return this.ReferenceSet.Contains(reference);
        }

        public bool AddReference(string reference)
        {
            if ((reference != null) && (reference.Length > 0))
            {
                return this.ReferenceSet.Add(reference);
            }
            else
            {
                return false;
            }
        }

        public string[] GetReferences()
        {
            return this.ReferenceSet.ToArray<string>();
        }

        public bool RemoveReference(string reference)
        {
            return this.ReferenceSet.Remove(reference);
        }

        public void SerializeAttributes()
        {
            this.References = SerializeUtil.SerializeArray(this.GetReferences());
        }

        public void DeSerializeAttributes()
        {
            string[] referenceArray = SerializeUtil.DeSerializeString(this.References);
            for (int i = 0; i < referenceArray.Length; i++)
            {
                this.AddReference(referenceArray[i]);
            }
        }
    }
}
